<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\recruitment\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\recruitment\model\recruitment;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 招聘列表逻辑层
 */
class recruitmentLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new recruitment();
    }
    public function getRecruitment($userId,$limit){
        return $this->model
            ->rightJoin('eb_collection', 'eb_recruitment.recruitment_id = eb_collection.collection_id')
            ->field('eb_recruitment.recruitment_id,eb_recruitment.position_name,eb_recruitment.salary_range,eb_recruitment.enterprise_id,eb_recruitment.work_experience,eb_recruitment.province_id,eb_recruitment.education_requirement,eb_recruitment.create_time')
            ->where('eb_collection.user_id', $userId)
            ->where('is_display',1)
            ->whereNull('eb_recruitment.delete_time')
            ->where('eb_collection.type', 3)
            // 修正拼写错误，将 ceeate_time 改为 create_time
            ->order('eb_collection.create_time desc')
            ->paginate($limit);;
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
        // var_dump($data);exit();

        $file_name = '招聘数据.xlsx';
        $header = [ '职位名称', '招聘企业','详情', '工作城市', '薪资范围', 
        '工作经验', '招聘人数', '学历要求', '创建时间','是否展示'];

        $filter = [
            // 'status' => [
            //     ['value' => 1, 'label' => '正常'],
            //     ['value' => 2, 'label' => '禁用']
            // ]
        ];
        $data_attr=[];
        foreach($data as $value){
           $data_attr[] = [
            'position_name'=>$value['position_name'],
            'enterprise_id_text'=>$value['enterprise_id_text'],
            'position_description'=>$value['position_description'],
            'province'=>$value['province_id_text'].'-'.$value['city_id_text'].'-'.$value['district_id_text'],
            'salary_range_text'=>$value['salary_range_text'],
            'work_experience_text'=>$value['work_experience_text'],
            'recruitment_number'=>$value['recruitment_number'],
            'education_requirement_text'=>$value['education_requirement_text'],
            'create_time'=>$value['create_time'],
            'is_display'=>$value['is_display']==1?'是':'否'
           ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 20,20,20,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }

}
